package swordoffer.offer18;


public class SwordOffer {
    public ListNode deleteNode(ListNode head, ListNode toBeDeleted){
        // base case1 : 链表中只有一个节点
        if (head == toBeDeleted) {
            return null;
        }
        // base case2 : 待删除节点不是尾节点
        if (toBeDeleted.next != null) {
            toBeDeleted.val = toBeDeleted.next.val;
            toBeDeleted.next = toBeDeleted.next.next;
        } else {
            // base case3 : 待删除节点是尾节点
            ListNode cur = head;
            while (cur.next != toBeDeleted) {
                cur = cur.next;
            }
            cur.next = null;
        }
        return head;
    }
}
